package com.hxzy.controller.admin;

import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.resp.R;
import com.hxzy.controller.admin.vo.mapstruct.EmployeeConvert;
import com.hxzy.controller.admin.vo.req.EmployeeReq;
import com.hxzy.controller.admin.vo.resp.EmployeeResp;
import com.hxzy.entity.Employee;
import com.hxzy.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 功能描述: springmvc 管理后台-员工管理
 *
 * @author tonneyyy
 */
@RestController
@RequestMapping(value = "/api/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService  employeeService;

    @GetMapping(value = "/{id}")
    public R<EmployeeResp> findById(@PathVariable(value = "id") Long id){
        Employee  db=this.employeeService.getById(id);
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        //MapStruct转
        EmployeeResp convert = EmployeeConvert.INSTANCE.convert(db);
        return R.okHasData(convert);
    }

    /**
     * 分页查询
     * @Valid启动 hibernate-validator数据验证操作 (spring aop)
     * @param req
     * @return
     */
    @GetMapping(value = "/page")
    public R<PageInfo<EmployeeResp>> search(@Valid  EmployeeReq req){
        PageInfo<EmployeeResp> pageInfo=this.employeeService.search(req);
        return R.okHasData(pageInfo);
    }

}
